/*
 * Copyright (c) 2020
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-08-13     chao         first version
 */

#ifndef __LCD_PORT_H__
#define __LCD_PORT_H__

#include <stdint.h>

#define LCD_WIDTH               240
#define LCD_HEIGHT              320
#define LCD_BITS_PER_PIXEL      16
#define LCD_FRAMEBUFFER_SIZE    (LCD_WIDTH * LCD_HEIGHT * LCD_BITS_PER_PIXEL / 8)
#define LCD_PIXEL_FORMAT        RTGRAPHIC_PIXEL_FORMAT_RGB565

#define LCD_BACKLIGHT_USING_GPIO
#define LCD_BL_GPIO_NUM     GET_PIN(B, 0)


//Regulative Command Set 
#define CMD_DriverCodeRead                              ((uint16_t)0x0000)
#define CMD_SoftwareReset                               ((uint16_t)0x0001)
#define CMD_ReadDisplayIDInfo                           ((uint16_t)0x0004)
#define CMD_ReadDisplayStatus                           ((uint16_t)0x0009)
#define CMD_ReadDisplayPowerMode                        ((uint16_t)0x000A)
#define CMD_ReadDisplayMADCTL                           ((uint16_t)0x000B)
#define CMD_ReadDisplayPixelFormat                      ((uint16_t)0x000C)
#define CMD_ReadDisplayImageFormat                      ((uint16_t)0x000D)
#define CMD_ReadDisplaySignalMode                       ((uint16_t)0x000E)
#define CMD_ReadDisplaySelfDiagnosticResult             ((uint16_t)0x000F)
#define CMD_EnterSleepMode                              ((uint16_t)0x0010)
#define CMD_SleepOUT                                    ((uint16_t)0x0011)
#define CMD_PartialModeON                               ((uint16_t)0x0012)
#define CMD_NormalDisplayModeON                         ((uint16_t)0x0013)
#define CMD_DisplayInversionOFF                         ((uint16_t)0x0020)
#define CMD_DisplayInversionON                          ((uint16_t)0x0021)
#define CMD_GammaSet                                    ((uint16_t)0x0026)
#define CMD_DisplayOFF                                  ((uint16_t)0x0028)
#define CMD_DisplayON                                   ((uint16_t)0x0029)
#define CMD_ColumnAddressSet                            ((uint16_t)0x002A)
#define CMD_PageAddressSet                              ((uint16_t)0x002B)
#define CMD_MemoryWrite                                 ((uint16_t)0x002C)
#define CMD_ColorSET                                    ((uint16_t)0x002D)
#define CMD_MemoryRead                                  ((uint16_t)0x002E)
#define CMD_PartialArea                                 ((uint16_t)0x0030)
#define CMD_VerticalScrollingDefinition                 ((uint16_t)0x0033)
#define CMD_TearingEffectLineOFF                        ((uint16_t)0x0034)
#define CMD_TearingEffectLineON                         ((uint16_t)0x0035)
#define CMD_MemoryAccessControl                         ((uint16_t)0x0036)
#define CMD_VerticalScrollingStartAddress               ((uint16_t)0x0037)
#define CMD_NIdleModeOFF                                ((uint16_t)0x0038)
#define CMD_NIdleModeON                                 ((uint16_t)0x0039)
#define CMD_PixelFormatSet                              ((uint16_t)0x003A)
#define CMD_WriteMemoryContinue                         ((uint16_t)0x003C)
#define CMD_ReadMemoryContinue                          ((uint16_t)0x003E)
#define CMD_SetTearScanline                             ((uint16_t)0x0044)
#define CMD_GetScanline                                 ((uint16_t)0x0045)
#define CMD_WriteDisplayBrightness                      ((uint16_t)0x0051)
#define CMD_ReadDisplayBrightness                       ((uint16_t)0x0052)
#define CMD_WriteCTRLDisplay                            ((uint16_t)0x0053)
#define CMD_ReadCTRLDisplay                             ((uint16_t)0x0054)
#define CMD_WriteContentAdaptiveBrightnessControl       ((uint16_t)0x0055)
#define CMD_ReadContentAdaptiveBrightnessControl        ((uint16_t)0x0056)
#define CMD_WriteCABCMinimumBrightness                  ((uint16_t)0x005E)
#define CMD_ReadCABCMinimumBrightness                   ((uint16_t)0x005F)
#define CMD_ReadID1                                     ((uint16_t)0x00DA)
#define CMD_ReadID2                                     ((uint16_t)0x00DB)
#define CMD_ReadID3                                     ((uint16_t)0x00DC)
//Extended Command Set
#define CMD_RGBInterfaceSignalControl                   ((uint16_t)0x00B0)
#define CMD_FrameControlInNormalMode                    ((uint16_t)0x00B1)
#define CMD_FrameControlInIdleMode                      ((uint16_t)0x00B2)
#define CMD_FrameControlInPartialMode                   ((uint16_t)0x00B3)
#define CMD_DisplayInversionControl                     ((uint16_t)0x00B4)
#define CMD_BlankingPorchControl                        ((uint16_t)0x00B5)
#define CMD_DisplayFunctionControl                      ((uint16_t)0x00B6)
#define CMD_EntryModeSet                                ((uint16_t)0x00B7)
#define CMD_BacklightControl1                           ((uint16_t)0x00B8)
#define CMD_BacklightControl2                           ((uint16_t)0x00B9)
#define CMD_BacklightControl3                           ((uint16_t)0x00BA)
#define CMD_BacklightControl4                           ((uint16_t)0x00BB)
#define CMD_BacklightControl5                           ((uint16_t)0x00BC)
#define CMD_BacklightControl7                           ((uint16_t)0x00BE)
#define CMD_BacklightControl8                           ((uint16_t)0x00BF)
#define CMD_PowerControl1                               ((uint16_t)0x00C0)
#define CMD_PowerControl2                               ((uint16_t)0x00C1)
#define CMD_VCOMControl1                                ((uint16_t)0x00C5)
#define CMD_VCOMControl2                                ((uint16_t)0x00C7)
#define CMD_NVMemoryWrite                               ((uint16_t)0x00D0)
#define CMD_NVMemoryProtectionKey                       ((uint16_t)0x00D1)
#define CMD_NVMemoryStatusRead                          ((uint16_t)0x00D2)
#define CMD_ReadID4                                     ((uint16_t)0x00D3)
#define CMD_PositiveGammaCorrection                     ((uint16_t)0x00E0)
#define CMD_NegativeGammaCorrection                     ((uint16_t)0x00E1)
#define CMD_DigitalGammaControl1                        ((uint16_t)0x00E2)
#define CMD_DigitalGammaControl2                        ((uint16_t)0x00E3)
#define CMD_InterfaceControl                            ((uint16_t)0x00F6)
//Description of Extend register command
#define CMD_PowercontrolA                               ((uint16_t)0x00CB)
#define CMD_PowercontrolB                               ((uint16_t)0x00CF)
#define CMD_DriverTimingControlA                        ((uint16_t)0x00E8)
#define CMD_DriverTimingControlB                        ((uint16_t)0x00EA)
#define CMD_PowerOnSequenceControl                      ((uint16_t)0x00ED)
#define CMD_Enable3G                                    ((uint16_t)0x00F2)
#define CMD_PumpRatioControl                            ((uint16_t)0x00F7)

//MADCTL_Parm
/* These 3 bits control MCU to memory write/read direction. */
#define MX_MY_MV_BGR(x, y, v, bgr)                      ((uint16_t)(y<<7)|(x<<6)|(v<<5)|(bgr<<3))
#if 0
/* Row Address Order. User can select one of them */
#define MY_ROW_ADDRESS_ORDER_ON                         ((uint16_t)0x0080)
#define MY_ROW_ADDRESS_ORDER_OFF                        ((uint16_t)0x0000)
/* Column Address Order. User can select one of them */
#define MX_ROW_ADDRESS_ORDER_ON                         ((uint16_t)0x0040)
#define MX_ROW_ADDRESS_ORDER_OFF                        ((uint16_t)0x0000)
/* Row / Column Exchange. User can select one of them */
#define MV_Row_Column_Exchange_ON                       ((uint16_t)0x0020)
#define MV_Row_Column_Exchange_OFF                      ((uint16_t)0x0000)
#else
#define PHYSICAL_0COLUMN_PHYSICAL_0ROW                  ((uint16_t)0x0000)
#define PHYSICAL_0COLUMN_PHYSICAL_319ROW                ((uint16_t)0x0080)
#define PHYSICAL_239COLUMN_PHYSICAL_0ROW                ((uint16_t)0x0040)
#define PHYSICAL_239COLUMN_PHYSICAL_319ROW              ((uint16_t)0x00C0)
#define PHYSICAL_0ROW_PHYSICAL_0COLUMN                  ((uint16_t)0x0020)
#define PHYSICAL_319ROW_PHYSICAL_0COLUMN                ((uint16_t)0x00A0)
#define PHYSICAL_0ROW_PHYSICAL_239COLUMN                ((uint16_t)0x0060)
#define PHYSICAL_319ROW_PHYSICAL_239COLUMN              ((uint16_t)0x00E0)
#endif
/* LCD vertical refresh direction control. User can select one of them */
#define ML_Vertical_Refresh_Order_ON                    ((uint16_t)0x0010)
#define ML_Vertical_Refresh_Order_OFF                   ((uint16_t)0x0000)
/* Color selector switch control (0=RGB color filter panel, 1=BGR color filter panel). User can select one of them */
#define BGR_BGR_Order                                   ((uint16_t)0x0008)
#define BGR_RGB_Order                                   ((uint16_t)0x0000)
/* LCD horizontal refreshing direction control. User can select one of them */
#define MH_Horizontal_Refresh_Order_ON                  ((uint16_t)0x0004)
#define MH_Horizontal_Refresh_Order_OFF                 ((uint16_t)0x0000)


//Color
#define COLOR_RED                                       ((uint16_t)0xF800)
#define COLOR_GREEN                                     ((uint16_t)0x07E0)
#define COLOR_BLUE                                      ((uint16_t)0x001F)
#define COLOR_WHITE                                     ((uint16_t)0xFFFF)
#define COLOR_BLACK                                     ((uint16_t)0x0000)
#define COLOR_BRED                                      ((uint16_t)0XF81F)           
#define COLOR_GRED                                      ((uint16_t)0XFFE0)
#define COLOR_GBLUE                                     ((uint16_t)0X07FF)
#define COLOR_MAGENTA                                   ((uint16_t)0xF81F)
#define COLOR_CYAN                                      ((uint16_t)0x7FFF)
#define COLOR_YELLOW                                    ((uint16_t)0xFFE0)
#define COLOR_BROWN                                     ((uint16_t)0XBC40) 
#define COLOR_BRRED                                     ((uint16_t)0XFC07) 
#define COLOR_GRAY                                      ((uint16_t)0X8430) 

#define COLOR_DARKBLUE                                  ((uint16_t)0X01CF) 
#define COLOR_LIGHTBLUE                                 ((uint16_t)0X7D7C) 
#define COLOR_GRAYBLUE                                  ((uint16_t)0X5458) 
 
#define COLOR_LIGHTGREEN                                ((uint16_t)0X841F) 
#define COLOR_LGRAY                                     ((uint16_t)0XC618) 

#define COLOR_LGRAYBLUE                                 ((uint16_t)0XA651) 
#define COLOR_LBBLUE                                    ((uint16_t)0X2B12) 


void lcd_write_cmd(uint16_t Command);
void lcd_write_param(uint16_t Parameter);
uint16_t lcd_read_param(void);
void lcd_set_window(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey);
void turn_on_lcd_backlight(void);
uint32_t lcd_framebuffer_addr(void);


#endif /* __LCD_PORT_H__ */


